<template>
  <div class="app-container">
    <!-- <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统属性" prop="isSystem">
        <el-select v-model="queryParams.isSystem" placeholder="请选择" clearable size="small">
          <el-option label="系统" value="1" />
          <el-option label="非系统" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="isDel">
        <el-select v-model="queryParams.isDel" placeholder="请选择" clearable size="small">
          <el-option label="启用" value="1" />
          <el-option label="禁用" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-waves="['system:param:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="paramList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="父级名称" align="center" prop="pid">
        <template slot-scope="{row}">
          {{ row.params.pName }}
        </template>
      </el-table-column>
      <el-table-column label="属性" align="center" prop="isSystem">
        <template slot-scope="{row}">
          <el-tag>
            {{ row.isSystem === 1 ? '系统': '非系统' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="isDel">
        <template slot-scope="{row}">
          <el-tag>
            {{ row.isDel === 1 ? '启用': '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="解释、备注" align="center" prop="text" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-waves="['system:param:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-waves="['system:param:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改参数字典对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item v-if="title === '添加参数字典'" label="父级id" prop="pid">
          <SelectTree
            placeholder="请选择父级id"
            :options="selectId"
            :props="props"
            :value="form.pid"
            @getValue="getValue($event)"
          />
        </el-form-item>
        <el-form-item label="系统属性">
          <el-radio-group v-model="form.isSystem">
            <el-radio :label="1">系统</el-radio>
            <el-radio :label="0">非系统</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.isDel">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="orderBy">
          <el-input v-model="form.orderBy" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="解释、备注" prop="text">
          <el-input v-model="form.text" placeholder="请输入解释、备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// listParamId
import { listParam, getParam, delParam, addParam, updateParam } from '@/api/xlsx/param'
import waves from '@/directive/waves'
import SelectTree from '@/components/TreeSelect'

export default {
  name: 'Param',
  components: { SelectTree },
  directives: { waves },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 参数字典表格数据
      paramList: [],
      // 参数字典树选项
      paramOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        code: null,
        name: null,
        pid: null,
        isSystem: null,
        isDel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: '编码不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      },
      // 父级id列表
      selectId: [],
      props: { // 配置项（必选）
        value: 'id',
        label: 'name',
        children: 'children'
        // disabled:true
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询参数字典列表 */
    getList() {
      this.loading = true
      listParam(this.queryParams).then(response => {
        // this.roleList = response.rows
        // this.total = response.total
        this.paramList = response.rows
        this.total = response.total
        this.loading = false
        this.getListId()
      })
    },
    // 获取所有父级id
    getListId() {
      listParam({
        pageNum: 1,
        pageSize: 1000
      }).then(response => {
        console.log(response.rows)
        this.selectId = response.rows
        console.log(this.selectId)
      })
    },
    // 选择的id
    getValue(value) {
      this.form.pid = value
      console.log(this.form.pid)
    },
    /** 转换参数字典数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      listParam().then(response => {
        this.paramOptions = []
        const data = { id: 0, name: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'id', 'pid')
        this.paramOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        pid: null,
        defaultStatus: 0,
        isSystem: 0,
        isDel: 0,
        orderBy: null,
        text: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      // this.getTreeselect()
      this.open = true
      this.title = '添加参数字典'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      // this.getTreeselect()
      if (row != null) {
        this.form.pid = row.id
      }
      getParam(row.id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改参数字典'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateParam(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addParam(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delParam(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {})
    }
  }
}
</script>
